﻿using System.Collections.Generic;
using System.Globalization;

namespace zijian666.Converts.Core;

/// <summary>
/// 资源管理器
/// </summary>
public static class ResourceStringManager
{
    /// <summary>
    /// 默认资源包
    /// </summary>
    private static ResourceStrings DEFAULT;
    /// <summary>
    /// 已注册的资源包
    /// </summary>
    public static readonly Dictionary<CultureInfo, ResourceStrings> _resources;

    static ResourceStringManager()
    {
        DEFAULT = new();
        _resources = new();
        Register(new CultureInfo("zh-CN"), new());
    }

    /// <summary>
    /// 根据区域信息获取资源包
    /// </summary>
    public static ResourceStrings GetResource(CultureInfo? cultureInfo)
    {
        if (cultureInfo is null || cultureInfo == CultureInfo.CurrentCulture || cultureInfo == CultureInfo.CurrentUICulture)
        {
            return DEFAULT;
        }

        if (_resources.TryGetValue(cultureInfo, out var resource))
        {
            return resource;
        }

        return DEFAULT;
    }
    /// <summary>
    /// 获取默认的资源包
    /// </summary>
    public static ResourceStrings GetResource() => GetResource(CultureInfo.CurrentUICulture);

    /// <summary>
    /// 注册资源包
    /// </summary>
    /// <param name="cultureInfo"></param>
    /// <param name="resource"></param>
    public static void Register(CultureInfo cultureInfo, ResourceStrings resource) => _resources.Add(cultureInfo, resource);

    /// <summary>
    /// 注册默认的资源包
    /// </summary>
    /// <param name="resource"></param>
    public static void RegisterDefault(ResourceStrings resource) => _resources.Add(CultureInfo.CurrentCulture, DEFAULT = resource);
}
